package tako.wisers.com.android.apis.handler;

import java.util.List;

import android.app.Dialog;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;

import tako.wisers.com.android.apis.activity.Tako;
import tako.wisers.com.android.apis.adapter.ListManager;
import tako.wisers.com.android.apis.database.SQListManager;
import tako.wisers.com.android.apis.event.Event;
import tako.wisers.com.android.apis.http.PanoramioService;
import tako.wisers.com.android.apis.item.PanoramioItem;
import tako.wisers.com.android.apis.location.GPSManager;
import tako.wisers.com.android.apis.location.Geohash;
import tako.wisers.com.android.apis.location.OverlayControl;
import tako.wisers.com.android.apis.util.Global;
import tako.wisers.com.android.apis.view.DialogManager;

public class PanoramioHandler extends TakoHandler implements Handler<Event>{

	private ListManager mImageManager;
	private MapView mMapView;
	private PanoramioService mService;
	private OverlayControl mOverlay;
	private static final String FILTE = "mKeyword='%s'";


	public PanoramioHandler(Tako tako) {
		super(tako);
		mMapView = tako.getMapView();
		mImageManager = ListManager.getInstance(tako);
		mService = new PanoramioService(tako);
		mOverlay = GPSManager.getInstance().getOverlayControl();
	}

	@SuppressWarnings("unchecked")
	public void execute(Event event) {
		SQListManager mSQListM = SQListManager.getInstance();
		Geohash encoder = new Geohash();
		GeoPoint point = mMapView.getMapCenter();
		
		mImageManager.clear();
		mOverlay.clearOverlay();
		
		String keyword = encoder.encode(point.getLatitudeE6(), point
				.getLongitudeE6(), Geohash.PHOTOBIT);
		String selection = String.format(FILTE, keyword);
		List<PanoramioItem> items = mSQListM.select(PanoramioItem.class,
				SQListManager.TABLE_INMEMORY, selection);
		if (items == null || items.size() == 0) {
			items = mService.getPanotamioItems(point);
			System.gc();
			mSQListM.insert(items, SQListManager.TABLE_INMEMORY);
		}
		if (items == null || items.size() == 0) {
			mhandler.sendMessage(mhandler.obtainMessage(SEARCH_FASILER));
		} else {
			mImageManager.loadPanoramio(items);
			int size = items.size();
			mOverlay.clearOverlay();
			for (int i = 0; i < size; i++) {
				PanoramioItem item = items.get(i);
				mOverlay.setOverlay(item.getLocation(), i);
			}
			mhandler.sendMessage(mhandler.obtainMessage(SEARCH_SUCCESS));
		}
		System.gc();
	}

	private android.os.Handler mhandler = new android.os.Handler() {
		@Override
		public void handleMessage(android.os.Message msg) {
			tako.dismissDialog();
			switch (msg.what) {
			case SEARCH_SUCCESS:
				mOverlay.setMyLocationOverlay(tako);
				mOverlay.setZoom(mOverlay.getZoom());
				mMapView.invalidate();
				break;
			case SEARCH_FASILER:
				DialogManager dm = new DialogManager(tako);
				Dialog dialog = dm.build(Global.WAITING_DIALOG);
				dialog.show();
				break;
			default:
				break;
			}
			mOverlay.Invalidate();
		}
	};

}
